package com.htandroid.htandroid.aty;

/**
 * Created by Administrator on 2015/12/26.
 */

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbLoadDialogFragment;
import com.ab.util.AbToastUtil;
import com.ab.view.ioc.AbIocView;
import com.ab.view.titlebar.AbTitleBar;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.entity.GcwxList;
import com.htandroid.htandroid.entity.GcwxVouchsPersons;
import com.htandroid.htandroid.sys.Basenvo;

import java.util.ArrayList;

public class Gcwx_persons extends AbActivity {
    private GcwxList vouch;
    private GcwxVouchsPersons vouchpersons;
    private ArrayList<GcwxVouchsPersons> vouchspersonslist;
    private String listtype = "0";
    private String ctype = "xd";
    private int icurrentrow;

    private AbTitleBar mAbTitleBar = null;
    private View rightView1 = null;
    private View rightView2 = null;
    private View rightView3 = null;
    private TextView tv_currentrow;
    private AbLoadDialogFragment mDialogFragment = null;

    @AbIocView(id = R.id.workhours_et)
    EditText workhours_et;
    @AbIocView(id = R.id.iwxtypescore_et)
    EditText iwxtypescore_et;
    @AbIocView(id = R.id.cmemo_et)
    EditText cmemo_et;
    @AbIocView(id = R.id.cwxpcode_tv)
    TextView cwxpcode_tv;
    @AbIocView(id = R.id.cwxpname_tv)
    TextView cwxpname_tv;
    @AbIocView(id = R.id.cwxptype_tv)
    TextView cwxptype_tv;
    @AbIocView(id = R.id.btn3_tv, click = "btnClick")
    TextView btn3_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.layout_gcwx_persons);
        initview();
        //已有材料显示,没有则默认增加一行并显示
        if (vouchspersonslist != null && vouchspersonslist.size() > 0) {
            insert_data(icurrentrow);
        }
    }

    public void btnClick(View v) {
        btn3_click(v);
    }

    //初始化
    private void initview() {
        mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("维修工时");
        mAbTitleBar.setLogo(R.drawable.icon_back);
        mAbTitleBar.getLogoView().setLayoutParams(new LinearLayout.LayoutParams(58, 57));
        mAbTitleBar.getLogoView().setFocusable(true);
        mAbTitleBar.getLogoView().setFocusableInTouchMode(true);
        mAbTitleBar.setTitleBarBackground(R.color.common_title);
        mAbTitleBar.setTitlePadding(118, 0, 0, 0);

        mAbTitleBar.clearRightView();
        rightView1 = mInflater.inflate(R.layout.btn_abs_back, null);
        mAbTitleBar.addRightView(rightView1);
        rightView2 = mInflater.inflate(R.layout.tv_currentrow, null);
        mAbTitleBar.addRightView(rightView2);
        rightView3 = mInflater.inflate(R.layout.btn_abs_forward, null);
        mAbTitleBar.addRightView(rightView3);
        tv_currentrow = (TextView) rightView2.findViewById(R.id.tv_currentrow);
        //点行号,进入明细列表的监听
        tv_currentrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                more_list(arg0);
            }
        });

        //获取传递的对象参数
        Intent intent = getIntent();
        vouchspersonslist = (ArrayList<GcwxVouchsPersons>) intent.getSerializableExtra("vouchspersonslist");
        vouch = (GcwxList) intent.getSerializableExtra("vouch");
        listtype = intent.getExtras().getString("listtype");
        ctype = intent.getExtras().getString("ctype");
        icurrentrow = getIntent().getExtras().getInt("currentrow");
        iwxtypescore_et.setVisibility(View.GONE);
    }

    //为窗口填充数据
    public void insert_data(int row) {
        vouchpersons = vouchspersonslist.get(row);
        icurrentrow = row;
        tv_currentrow.setText((icurrentrow + 1) + "/" + (vouchspersonslist.size()));
        //增加/删除按钮
        if (listtype.equals("0")) {
            showwin(true);
        } else {
            showwin(false);
        }
        //写入数据
        cwxpcode_tv.setText(vouchpersons.getCwxpcode());
        cwxpname_tv.setText(vouchpersons.getCwxpname());
        cwxptype_tv.setText(vouchpersons.getCwxptypename());
        cwxptype_tv.setTag(vouchpersons.getCwxptype());
        workhours_et.setText(Basenvo.customFormat("######.00##", vouchpersons.getWorkhours()));
        iwxtypescore_et.setText(Basenvo.customFormat("######.00##", vouchpersons.getIwxtypescore()));
        cmemo_et.setText(vouchpersons.getCmemo());
    }

    public void showwin(boolean stype) {
        //背景变白，字变黑色
        Integer backgroundcolor = Color.TRANSPARENT;
        if (ctype.equals("check") && stype) {
            stype = false;
        }
        workhours_et.setEnabled(stype);
        iwxtypescore_et.setEnabled(stype);
        cmemo_et.setEnabled(stype);
        if (stype) {
            workhours_et.setBackgroundResource(R.drawable.edit_background);
            iwxtypescore_et.setBackgroundResource(R.drawable.edit_background);
            cmemo_et.setBackgroundResource(R.drawable.edit_background);
            //隐藏修改,删除确定
            btn3_tv.setVisibility(View.VISIBLE);
        } else {
            workhours_et.setBackgroundColor(backgroundcolor);
            iwxtypescore_et.setBackgroundColor(backgroundcolor);
            cmemo_et.setBackgroundColor(backgroundcolor);
            //隐藏修改,删除确定
            btn3_tv.setVisibility(View.GONE);
        }
    }

    //存货切换
    public void more_list(View v) {
        updatedataref();
        if (!prechecked()) return;
        Intent intent = new Intent(getApplicationContext(), Gcwx_persons_detaillist.class);
        //传递参数
        Bundle bundle = new Bundle();
        intent.putExtra("currentrow", "" + icurrentrow);//当前行号
        bundle.putSerializable("vouchspersonslist", vouchspersonslist);//存货明细列表
        intent.putExtras(bundle);
        startActivityForResult(intent, R.layout.layout_gcwx_inventory_detaillist / 500000);
    }

    //确定返回上一页面
    public void btn3_click(View v) {
        updatedataref();
        if (!prechecked()) return;
        Intent intent = new Intent(getApplicationContext(), Gcwx_xd_detail.class);
        //传递参数
        Bundle bundle = new Bundle();
        bundle.putSerializable("vouchspersonslist", vouchspersonslist);
        intent.putExtras(bundle);
        setResult(R.layout.layout_gcwx_persons / 400000, intent);  //设置返回结果
        //打开延缓窗口,关闭当前窗口
        this.finish();
    }

    //对一些修改后的值进行处理,避免因为没触发事件导致保存的信息不正确
    public void updatedataref() {
        //保存修改内容
        if (workhours_et.getText() != null) {
            vouchpersons.setWorkhours(workhours_et.getText().toString());
        }
        if (iwxtypescore_et.getText() != null) {
            vouchpersons.setIwxtypescore(iwxtypescore_et.getText().toString());
        }
        if (cmemo_et.getText() != null) {
            vouchpersons.setCmemo(cmemo_et.getText().toString());
        }
    }

    //检测数据是否合法
    public boolean prechecked() {
        return true;
    }

    //向左翻页
    public void btn_abs_back(View v) {
        updatedataref();
        if (!prechecked()) return;
        if (icurrentrow == 0) {
            AbToastUtil.showToastInThread(getApplicationContext(), "已经是第一项!");
        } else {
            insert_data(icurrentrow - 1);
        }
    }

    //向右翻页
    public void btn_abs_forward(View v) {
        updatedataref();
        if (!prechecked()) return;
        if (icurrentrow == (vouchspersonslist.size() - 1)) {
            AbToastUtil.showToastInThread(getApplicationContext(), "已经是最后一项!");
        } else {
            insert_data(icurrentrow + 1);
        }
    }

    //接收返回数据(存货明细,行切换,成本对象)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case R.layout.layout_gcwx_inventory_detaillist / 500000:
                Bundle bundle = data.getExtras();
                String listrow = bundle.getString("listrow");
                insert_data(Integer.valueOf(listrow));
            default:
                //其它窗口的回传数据
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

